var BaseHandler     = require('../base-handler');
var HTTP_VERBS      = require('../../../constants').HTTP_VERBS;

class PlatformAuthorizationEventsHandler extends BaseHandler {

    get pattern() {
        return "/wechat/authorization/events";
    }

    doPOST(request, response) {
        this.Controller.handleWeChatAuthenticationEvent(request.query, request.body, (result) => {
            if (request.session) {
                request.session.destroy((error) => {
                    response.end('success');
                });
            } else {
                response.end('success');
            }
        });
    }

    doGET(request, response) {
        if (request.session) {
            request.session.destroy((error) => {
                response.end('success');
            });
        } else {
            response.end('success');
        }
    }

    setup() {
        this.registerVerb(HTTP_VERBS.POST, this.doPOST);
        this.registerVerb(HTTP_VERBS.GET, this.doGET);
    }
}

module.exports = PlatformAuthorizationEventsHandler;